package com.zzc.fwzpxt.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zzc.fwzpxt.entity.Article;
import com.zzc.fwzpxt.entity.Tag;
import com.zzc.fwzpxt.service.IArticleService;
import com.zzc.fwzpxt.service.ITagService;
import com.zzc.common.controller.BaseController;
import com.zzc.common.result.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @User Administrator
 * @Date Create in 19:33/2017/9/29
 * @Descriptton {
 *     文章控制器
 *  }
 **/

@Controller
public class BlogArticleController extends BaseController {

    private final int pageSise = 10;

    @Autowired
    private IArticleService iArticleService;
    @Autowired
    private ITagService iTagService;

    @RequestMapping("/article.html")
    public String news(Model model){
        Page<Article> page = new Page<>(1, pageSise);
        EntityWrapper<Article> articleEntityWrapper = new EntityWrapper<>();
        articleEntityWrapper.eq("status",1);
        articleEntityWrapper.eq("article_type", 5);
        articleEntityWrapper.orderBy("gmt_create", false);
        Page<Article> articlePage = iArticleService.selectPage(page, articleEntityWrapper);
        model.addAttribute("articlePage",articlePage);
        return "blog/article";
    }
    /**
     * 文章分页
     * @param model
     * @param pageNum
     * @return
     */
    @RequestMapping("/article/page/{pageNum}")
    public String newsPages(Model model, @PathVariable Integer pageNum){
        Page<Article> page = new Page<>(pageNum, pageSise);
        EntityWrapper<Article> articleEntityWrapper = new EntityWrapper<>();
        articleEntityWrapper.eq("status",1);
        articleEntityWrapper.eq("article_type", 5);
        articleEntityWrapper.orderBy("gmt_create", false);
        Page<Article> articlePage = iArticleService.selectPage(page, articleEntityWrapper);
        model.addAttribute("articlePage",articlePage);
        return "blog/article";
    }
    /**
     * 获取文章内容
     * @param id
     * @return
     */
    @RequestMapping(value = "/article/{id}.html")
    public String newsContent(Model model, @PathVariable Integer id) {
        // 查询文章
        Article article = iArticleService.selectById(id);
        model.addAttribute("article",article);
        // 查询上一篇
        Article prevArticle = iArticleService.findPrevArticle(article.getId(), 5l);
        model.addAttribute("prevArticle",prevArticle);
        // 查询下一篇
        Article nextArticle = iArticleService.findNextArticle(article.getId(), 5l);
        model.addAttribute("nextArticle",nextArticle);
        // 查询文章标签
        List<Tag> tags = iTagService.findTagsByArticleId(article.getId());
        model.addAttribute("tags",tags);
        return "blog/article_content";
    }

    /**
     * 查询行业资讯
     * @return
     */
    @GetMapping(value = {"/industry"})
    public String industry(Model model) {

        Page<Article> page = new Page<>(1, pageSise);
        EntityWrapper<Article> articleEntityWrapper = new EntityWrapper<>();
        articleEntityWrapper.eq("status", 1)
                            .eq("article_type", 6);
        Page<Article> articlePage = iArticleService.selectPage(page, articleEntityWrapper);
        model.addAttribute("articlePage", articlePage);
        return "/blog/industry";
    }

    /**
     * 查询行业资讯分页
     * @return
     */
    @GetMapping(value = "/industry/page/{id}")
    public String industryPage(Model model, @PathVariable int id) {

        Page<Article> page = new Page<>(id, pageSise);
        EntityWrapper<Article> articleEntityWrapper = new EntityWrapper<>();
        articleEntityWrapper.eq("status", 1)
                .eq("article_type", 6);
        Page<Article> articlePage = iArticleService.selectPage(page, articleEntityWrapper);
        model.addAttribute("articlePage", articlePage);
        return "/blog/industry";
    }

    /**
     * 获取文章内容
     * @param id
     * @return
     */
    @RequestMapping(value = "/industry/{id}.html")
    public String industryContent(Model model, @PathVariable Integer id) {
        // 查询文章
        Article article = iArticleService.selectById(id);
        model.addAttribute("article",article);
        // 查询上一篇
        Article prevArticle = iArticleService.findPrevArticle(article.getId(), 6l);
        model.addAttribute("prevArticle",prevArticle);
        // 查询下一篇
        Article nextArticle = iArticleService.findNextArticle(article.getId(), 6l);
        model.addAttribute("nextArticle",nextArticle);
        // 查询文章标签
        List<Tag> tags = iTagService.findTagsByArticleId(article.getId());
        model.addAttribute("tags",tags);
        return "blog/industry_content";
    }

}
